package cn.slipi.core.configuration;

import io.netty.channel.ChannelOption;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.stereotype.Component;
import reactor.netty.resources.LoopResources;

@Component
public class ContainerConfiguration extends ReactiveWebServerFactoryCustomizer {

    public ContainerConfiguration(ServerProperties serverProperties) {
        super(serverProperties);
    }

    @Override
    public void customize(ConfigurableReactiveWebServerFactory factory) {
        super.customize(factory);
        NettyReactiveWebServerFactory nettyFactory = (NettyReactiveWebServerFactory) factory;
        nettyFactory.setResourceFactory(null);
        nettyFactory.addServerCustomizers(httpServer -> httpServer.tcpConfiguration(tcpServer ->
                tcpServer.runOn(LoopResources.create("gateway", Runtime.getRuntime().availableProcessors() * 4, Runtime.getRuntime().availableProcessors() * 8, true))
                        .selectorOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 200).option(ChannelOption.TCP_NODELAY, true)

        ));
    }
}
